function TypePanel(){
	
}

TypePanel.instance = TypePanel;

TypePanel.faceUrl = "typeController";

TypePanel.typeDatagrid = null;

TypePanel.typeEditWindow = null;

TypePanel.init = function(){
	TypePanel.typeDatagrid = mini.get("typeDatagrid");
	TypePanel.typeEditWindow = mini.get("typeEditWindow");
	TypePanel.reload();
};

//刷新
TypePanel.reload = function(){
	TypePanel.loadData();
};

//增加
TypePanel.add = function(){
	var form = new mini.Form("#typeEditform");
	form.clear();
	TypePanel.typeEditWindow.show();
};

//编辑
TypePanel.edit = function(){
	var rowData = TypePanel.typeDatagrid.getSelected();
	
	var form = new mini.Form("#typeEditform");
	form.clear();
	if(rowData){
		form.setData(rowData);
		TypePanel.typeEditWindow.show();
	}else{
		mini.alert("请选中要修改的类别");
	}
};

//删除
TypePanel.del = function() {
	
	var rowData = TypePanel.typeDatagrid.getSelected();
	
	if(!rowData){
		mini.alert("请选中要删除的记录！");
	}else{
		mini.confirm("确定要删除类别名为  " + "<span style='color: red;'> " +rowData.name + "</span>" + "  的类别吗？", "删除类别",
	            function (action) {      
	                if (action == "ok") {
	                	var ids = new Array();
	            		ids.push(rowData.typeId);
	            		$.ajax( {
	            			type: 'POST',
	            			dataType: 'json',
	            			url: __root+"/"+TypePanel.faceUrl,
	            			data: {
	            				method: 'delete',
	            				typeId: ids,
	            				lslUser: gbOperateUser,
				    			lslAddress: gbOperateIp
	            			},
	            			success: function(text) {
	            				TypePanel.reload();
	            			},
	            			error: function(obj, error) {
	            				TypePanel.reload();
	            			}
	            		});
	                }
		});
	}
};

//关闭窗口
TypePanel.closeButton = function(){
	TypePanel.typeEditWindow.hide();
};

/**
 * 查找数据
 */
TypePanel.loadData = function() {

	var params = {};
	params.method = 'find';
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+TypePanel.faceUrl,
		cache: false,
		data: params,
		success: function(json) {
			TypePanel.typeDatagrid.loadData(json);
		},
		error: function(obj, ertxt) { /* 失败回调函数.这里是网络失败, 比如500错误等 */
			mini.alert("初始化数据错误：" + ertxt);
		}
	});
};

/**
 * 保存
 */
TypePanel.save = function() {
	
	var form = new mini.Form("#typeEditform");
	
	form.validate();
    if (form.isValid() == false) return;
	
	var data = form.getData();
	data.method = "save";
	data.lslUser = gbOperateUser;
	data.lslAddress = gbOperateIp;

	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+TypePanel.faceUrl,
		data: data,
		success: function(text) {
			TypePanel.reload();
			TypePanel.closeButton();
			return;
		},
		error:function(){
			mini.alert("保存出错！");
			TypePanel.closeButton();
		}
	});
};
